//////////////////////////////////////////////////////////////////////////
#include <RocketLua/RocketLua.hpp>


using namespace Rocket::Core::Lua;


float Clamp( float v, float low, float high )
{
	return v >= low ? v <= high ? v : high : low;
}


Colorf::Colorf()
:	mR( 0 )
,	mG( 0 )
,	mB( 0 )
,	mA( 0 )
{
}


Colorf::Colorf( float r, float g, float b, float a )
:	mR( r )
,	mG( g )
,	mB( b )
,	mA( a )
{
}


Colorf& Colorf::Set( float r, float g, float b, float a )
{
	mR	=	r;
	mG	=	g;
	mB	=	b;
	mA	=	a;
	return *this;
}

bool Colorf::operator ==( const Colorf& rhs ) const
{
	return mR==rhs.mR && mG==rhs.mG && mB==rhs.mB && mA==rhs.mA;
}


Colorf Colorf::operator +( const Colorf& rhs ) const
{
	return Colorf( mR+rhs.mR, mG+rhs.mG, mB+rhs.mB, mA+rhs.mA );
}


Colorf Colorf::operator -( const Colorf& rhs ) const
{
	return Colorf( mR-rhs.mR, mG-rhs.mG, mB-rhs.mB, mA-rhs.mA );
}


Colorf Colorf::operator *( float rhs ) const
{
	return Colorf( mR*rhs, mG*rhs, mB*rhs, mA*rhs );
}


Colorf Colorf::operator /( float rhs ) const
{
	return Colorf( mR/rhs, mG/rhs, mB/rhs, mA/rhs );
}


Colorf Colorf::Clamp( float low, float high ) const
{
	return Colorf(
		::Clamp( mR, low, high ),
		::Clamp( mG, low, high ),
		::Clamp( mB, low, high ),
		::Clamp( mA, low, high )
	);
}


float Colorf::R() const
{
	return mR;
}


float Colorf::G() const
{
	return mG;
}


float Colorf::B() const
{
	return mB;
}


float Colorf::A() const
{
	return mA;
}


void Colorf::R( float r )
{
	mR	=	r;
}


void Colorf::G( float g )
{
	mG	=	g;
}


void Colorf::B( float b )
{
	mB	=	b;
}


void Colorf::A( float a )
{
	mA	=	a;
}


void Colorf::Register( lua_State* state )
{
	luabridge::getGlobalNamespace( state )
		.beginClass< Colorf >( "Colorf" )
		.addConstructor< void (*)( void ) >()
		.addProperty( "r", &Colorf::R, &Colorf::R )
		.addProperty( "g", &Colorf::G, &Colorf::G )
		.addProperty( "b", &Colorf::B, &Colorf::B )
		.addProperty( "a", &Colorf::A, &Colorf::A )
		.addFunction( "Set", &Colorf::Set )
		.addFunction( "Clamp", &Colorf::Clamp )
		.addFunction( "__eq", &Colorf::operator == )
		.addFunction( "__add", &Colorf::operator + )
		.addFunction( "__sub", &Colorf::operator - )
		.addFunction( "__mul", &Colorf::operator * )
		.addFunction( "__div", &Colorf::operator / )
		.endClass();
}
